#网易号
import pathlib
import time

from selenium import webdriver
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager

# 基本信息
# 视频存放路径
catalog_mp4 = r"C:\Users\Jingbit\Desktop\自媒体\剪映\成品\谨防食品中的“五毒”(1)"
# 视频描述
describe = "裸眼3D看《白蛇2：青蛇崛起》今日惊蛰，蛇虫横行，你我姐妹两条蛇，掀翻这金山寺 #青蛇 #裸眼3D #AI #国漫 #视觉震撼"
time.sleep(10)
# 创建Chrome浏览器服务
s = Service(ChromeDriverManager().install())
# 启动Chrome浏览器并打开指定网址
driver = webdriver.Chrome(service=s)

path = pathlib.Path(catalog_mp4)

# 视频地址获取
path_mp4 = ""
for i in path.iterdir():
    if ".mp4" in str(i):
        path_mp4 = str(i);
        break;

if path_mp4 != "":
    print("检查到视频路径：" + path_mp4)
else:
    print("未检查到视频路径，程序终止！")
    exit()

# 封面地址获取
path_cover = ""
for i in path.iterdir():
    if ".png" in str(i) or ".jpg" in str(i):
        path_cover = str(i);
        break;

if path_cover != "":
    print("检查到封面路径：" + path_cover)
else:
    print("未检查到封面路径，程序终止！")
    exit()


def login():
    driver.get("https://mp.sohu.com/mpfe/v3/main/news/addvideo")
    time.sleep(2)
    driver.find_element(By.XPATH, "//*[@id='mpfe']/div[3]/div[2]/section[1]/div[1]/div/div[3]/div[1]/input").send_keys(
        "13612935690")
    driver.find_element(By.XPATH, "//*[@id='mpfe']/div[3]/div[2]/section[1]/div[1]/div/div[3]/div[2]/input").send_keys(
        "Ps@19881103")
    driver.find_element(By.XPATH, "//*[@id='mpfe']/div[3]/div[2]/section[1]/div[1]/div/button").click()
    # 等待登录完成
    driver.implicitly_wait(10)
    exec_count = 0
    while exec_count < 15:
        try:
            new_window = driver.window_handles[-1]  # 找到新窗口名字
            driver.switch_to.window(new_window)  # 切换到最新打开的窗口
            ele_xpath = driver.find_element(By.XPATH, "//*[@id='header-user']/div[1]")
            if ele_xpath is None:
                exec_count += 1
                continue
            else:
                exec_count=16
            print("成功执行第{}次".format(exec_count))
        except WebDriverException as e:
            print("执行失败 :{}".format(e))
            continue
        time.sleep(1)
    publish_souhu()


def publish_souhu():
    '''
     作用：发布搜狐视频
    '''

    # 进入搜狐短视频创作者页面，并上传视频
    driver.get("https://mp.163.com/subscribe_v3/index.html#/video-publish")
    time.sleep(2)
    driver.find_element(By.XPATH, '//input[@type="file"]').send_keys(path_mp4)

    # 等待视频上传完成
    while True:
        time.sleep(3)
        try:
            driver.find_element(By.XPATH, '//*[text()="视频上传成功！"]')
            break;
        except Exception as e:
            print("视频还在上传中···")

    print("视频已上传完成！")

    # 输入标题，30个文字限制处理
    driver.find_element(By.XPATH, '//*[@class="publish-title"]//input').clear()
    time.sleep(2)
    title = describe[:describe.index(" #")]
    if (len(title) > 30):
        title = title[:27] + "..."
    driver.find_element(By.XPATH, '//*[@class="publish-title"]//input').send_keys(title)

    # 输入描述
    time.sleep(1)
    driver.find_element(By.XPATH, '//*[@class="abstract-main"]//textarea').send_keys(describe)

    # 添加封面
    time.sleep(1)
    driver.find_element(By.XPATH, '//*[text()="上传图片"]').click()
    time.sleep(1)
    driver.find_element(By.XPATH, '//input[@type="file" and @id="new-file"]').send_keys(path_cover)
    time.sleep(5)
    driver.find_element(By.XPATH, '//*[@class="sync-library"]/..//*[text()="确定"]').click()
    time.sleep(3)

    # 选择分类
    time.sleep(1)
    target = driver.find_element(By.XPATH, '//*[text()="频道分类"]/../div[1]//*[@class="select-main"]')
    # 滚动到目标位置
    driver.execute_script("arguments[0].scrollIntoView();", target)
    time.sleep(1)
    driver.find_element(By.XPATH, '//*[text()="频道分类"]/../div[1]//*[@class="select-main"]').click()
    time.sleep(2)
    driver.find_element(By.XPATH, '//*[text()="频道分类"]/../div[1]//*[contains(text(),"动漫")]').click()
    time.sleep(1)
    driver.find_element(By.XPATH, '//*[text()="频道分类"]/../div[2]//*[@class="select-main"]').click()
    time.sleep(2)
    driver.find_element(By.XPATH, '//*[text()="频道分类"]/../div[2]//*[contains(text(),"动漫")]').click()

    # 人工进行检查并发布
    # time.sleep(3)
    # # 点击发布
    # driver.find_element(By.XPATH,'//*[text()="发布"]').click()
    driver.quit()
    driver.close()


# 开始执行视频发布
login()
